package com.game;

import java.awt.*;
import java.util.ArrayList;

public abstract class Tank extends GameObject{
    //坦克大小
    private int width = 50;
    private int height = 50;
    //移动速度
    private int speed = 3;
    //坦克是否存活
    public boolean alive = false;
    //设置坦克初始方向 向上
    private Direction direction = Direction.UP;
    //坦克四个方向的图片
    private String upImg;
    private String leftImg;
    private String rightImg;
    private String downImg;

    //攻击冷却状态
    boolean attackCoolDown = true;
    //攻击冷却时间：ms
    int attackCoolDownTime = 1000;

    //构造方法
    public Tank(String img, int x, int y, GamePanel gamePanel, String upImg, String leftImg, String rightImg, String downImg) {
        super(img, x, y, gamePanel);
        this.upImg = upImg;
        this.leftImg = leftImg;
        this.rightImg = rightImg;
        this.downImg = downImg;
    }

    /*
     * 坦克移动
     */
    //向左移动
    public void leftWard() {
        //给坦克移动添加条件--坦克下一步的坐标
          if (!hitWall(x - speed,y) && !moveToBorder(x - speed,y) && !hitBot(x - speed,y)){
              x -= speed;
          }
        setImg(leftImg);
        direction = Direction.LEFT;
    }
    //向右移动
    public void rightWard(){
        if (!hitWall(x + speed,y) && !moveToBorder(x + speed,y) && !hitBot(x + speed,y)){
            x += speed;
        }
        setImg(rightImg);
        direction = Direction.RIGHT;
    }
    //向上移动
    public void upWard(){
        if (!hitWall(x,y - speed) && !moveToBorder(x,y - speed) && !hitBot(x,y - speed)){
            y -= speed;
        }
        setImg(upImg);
        direction = Direction.UP;
    }
    //向下移动
    public void downWard(){
        if (!hitWall(x,y + speed) && !moveToBorder(x,y + speed) && !hitBot(x,y + speed)){
            y += speed;
        }
        setImg(downImg);
        direction = Direction.DOWN;
    }

    /*
    * 坦克发射子弹
    * */
    public void attack(){
        //玩家存活且技能不冷却才可以攻击
       if (attackCoolDown && alive){
           //初始化子弹
           Point p = this.getHeadPoint();
           Bullet bullet = new Bullet("image/bullet/bulletGreen.gif",p.x-7,p.y-7,this.getGamePanel(),this.getDirection());
           //把初始化的子弹添加到列表中
           getGamePanel().bulletList.add(bullet);
           //启动线程
           new AttackCD().start();
       }
    }

    /*
    * 新线程：坦克冷却CD
    * */
    class AttackCD extends Thread{
        @Override
        public void run() {
            //将攻击设置为冷却
            attackCoolDown = false;
            try {
                Thread.sleep(attackCoolDownTime);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            //将攻击解除冷却
            attackCoolDown = true;
        }
    }

    /*
    * 获得子弹发射位置———坦克的头部
    * */
    public Point getHeadPoint(){
        switch (direction){
            case UP :
                return new Point(x + this.width/2,y);
            case LEFT:
                return new Point(x,y + this.height/2);
            case RIGHT:
                return new Point(x + this.width,y + this.height/2);
            case DOWN:
                return new Point(x + this.width/2,y + this.height);
            default:
                return null;
        }
    }

    /*
    * 坦克与墙壁的碰撞检测-先检测再移动
    * 用坦克下一步坐标进行碰撞检测
    * 给坦克移动方法添加条件
    * */
    public boolean hitWall(int nextX,int nextY){
        //坦克下一步的矩形
        Rectangle next = new Rectangle(nextX,nextY,width,height);
        //创建围墙列表
        ArrayList<Wall> walls = getGamePanel().wallList;
        for (Wall wall : walls){
            //与每一个围墙进行碰撞检测
            if (next.intersects(wall.gerRec())){
                //发生碰撞，返回true
                return true;
            }
        }
        //坦克与Fe墙的检测碰撞
        ArrayList<Fe> feList = getGamePanel().feList;
        for (Fe fe : feList) {
            if (fe.gerRec().intersects(next)){
                return true;
            }
        }
        return false;
    }

    /*
    * 坦克与边缘的检测碰撞
    * 给坦克移动方法添加条件
    * */
    public boolean moveToBorder(int nextX,int nextY){
        if (nextX < 10){
            return true;
        }
        else if (nextX + this.width + 10 > getGamePanel().width ){
            return true;
        }
        else if (nextY < 30){
            return true;
        }
        else if (nextY + this.height + 6 > getGamePanel().height ){
            return true;
        }
        return false;
    }

    /*
    * 坦克与坦克的检测碰撞
    * */
    public boolean hitBot(int nextX,int nextY){
        int a=0;
        //假设坦克前进，下一个位置形成的矩形
        Rectangle next = new Rectangle(nextX,nextY,width,height);
        //地图里所有的人机
        ArrayList<Bot> bots = getGamePanel().botlist;
        //玩家
        ArrayList<Tank> players = getGamePanel().playerList;
        for (Bot bot : bots) {
            if (bot.gerRec().intersects(next)){
                a++;
                if(a == 2) {
                    return true;
                }
            }
        }
        for (Tank player : players) {
            if (player.gerRec().intersects(next)){
                a++;
                if(a == 2) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void paintSelf(Graphics g) {

    }
    @Override
    public Rectangle gerRec() {
        return null;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public Direction getDirection() {
        return direction;
    }

    public void setDirection(Direction direction) {
        this.direction = direction;
    }

    public String getUpImg() {
        return upImg;
    }

    public void setUpImg(String upImg) {
        this.upImg = upImg;
    }

    public String getLeftImg() {
        return leftImg;
    }

    public void setLeftImg(String leftImg) {
        this.leftImg = leftImg;
    }

    public String getRightImg() {
        return rightImg;
    }

    public void setRightImg(String rightImg) {
        this.rightImg = rightImg;
    }

    public String getDownImg() {
        return downImg;
    }

    public void setDownImg(String downImg) {
        this.downImg = downImg;
    }


}
